import HTTP from '../../util/http-p.js';
const app = getApp();

export default class KeywordModel extends HTTP {
  key = 'q'
  maxLength = 10

  // 获取历史搜索
  getHistory() {
    let words = wx.getStorageSync(this.key);
    if (!words) return [];
    return words;
  }

  // 获取热门搜索
  getHot() {
    return this.request({url:app.globalData.url.book.hotKeyword})
  }

  // 将关键字写入缓存中
  addToHistory(keyword) {
    let words = this.getHistory();
    const has = words.includes(keyword);
    if (!has) {
      if (words.length >= this.maxLength) words.pop();
      words.unshift(keyword);
      wx.setStorageSync(this.key, words);
    }
  }
}